﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCore.Common.Helper;
using NetCore.IServices.User;
using NetCore.Model.Enity;
using NetCore.Model.User;
using NetCore.Model.VeiwModels;
using System.Threading.Tasks;

namespace NetCore.WebApi.Controllers
{
    public class UserController : BaseController
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpGet]
        public IActionResult Hello()
        {
            return Ok("Hello World");
        }

        /// <summary>
        /// 获取User实体
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [ApiExplorerSettings(IgnoreApi = true)]//Swagger中隐藏接口
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public IActionResult GetUserInfo(User user)
        {
            return Ok(user);
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="loginFrom"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(LoginFrom loginFrom)
        {
            string jwtStr = string.Empty;
            bool suc = false;
            //if (role != null)
            //{
            //    // 将用户id和角色名，作为单独的自定义变量封装进 token 字符串中。
            //    TokenModel tokenModel = new TokenModel { Uid = "abcde", Role = role };
            //    jwtStr = JwtHelper.IssueJwt(tokenModel);//登录，获取到一定规则的 Token 令牌
            //    suc = true;
            //}
            //else
            //{
            //    jwtStr = "login fail!!!";
            //}

            return Ok(new
            {
                loginFrom,
                success = suc,
                token = jwtStr
            });
        }

        /// <summary>
        /// 解析Token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public IActionResult ParseToken()
        {
            //需要截取Bearer
            var tokenHeader = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var user = JwtHelper.SerializeJwt(tokenHeader);
            return Ok(user);
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="id">参数id</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetUser(int id)
        {
            SysUser user = await _userService.QueryByID(id);
            return Ok(user);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> Add(SysUser user)
        {
            var count = await _userService.Add(user);
            return Ok(count);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> Update(SysUser user)
        {
            var sucess = await _userService.Update(user);
            return Ok(sucess);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> Delete(object[] ids)
        {
            var sucess = await _userService.DeleteByIds(ids);
            return Ok(sucess);
        }

        /// <summary>
        /// 获取用户条数
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetCount()
        {
            var count = await _userService.GetCount();
            return Ok(count);
        }

        /// <summary>
        /// 测试automapper
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> AutoMapper(int id)
        {
            var userinfo = await _userService.GetUserDetails(id);
            return Ok(userinfo);
        }

        /// <summary>
        /// 测试aop缓存redis
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> AopTest(int id)
        {
            var sucess = await _userService.GetUserDetails(id);
            return Ok(sucess);
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddEquipmentInfo(SysEquipmentinfo sysEquipmentinfo)
        {
            var sucess = await _userService.AddEquipmentInfo(sysEquipmentinfo);
            return Ok(sucess);
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetOrder_LayerwindingmachineEquipmentInfo()
        {
            var sucess = _userService.GetOrder_LayerwindingmachineEquipmentInfo();
            return Ok(sucess);
        }
    }
}